package com.push.spider.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.push.common.utils.DateUtils;
import com.push.common.utils.EncryptUtil;
import com.push.common.utils.LinkHelper;
import com.push.common.utils.StringUtils;
import com.push.common.utils.httpclient.HttpClientResult;
import com.push.common.utils.httpclient.HttpClientUtil;
import com.push.web.domain.BizArticle;
import com.push.web.domain.BizLabelSecond;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.net.URL;
import java.time.LocalDate;
import java.time.chrono.MinguoDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.commons.lang3.time.DateUtils.parseDate;

public class Test {
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
             "yyyy/MM/dd", "dd/MM/yyyy","yyyy-MM", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM",
            "yyyy.MM.dd G 'at' HH:mm:ss z", "EEE, MMM d, ''yy", "h:mm a",
            "hh 'o''clock' a, zzzz", "K:mm a, z", "yyyyy.MMMMM.dd GGG hh:mm aaa",
            "EEE, d MMM yyyy HH:mm:ss Z", "yyMMddHHmmssZ", "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
            "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "YYYY-'W'ww-u", "EEE, dd MMM yyyy HH:mm:ss z",
            "EEE, dd MMM yyyy HH:mm zzzz", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.SSSzzzz",
            "yyyy-MM-dd'T'HH:mm:sszzzz", "yyyy-MM-dd'T'HH:mm:ss z", "yyyy-MM-dd'T'HH:mm:ssz",
            "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HHmmss.SSSz", "yyyy-MM-dd",
            "yyyyMMdd", "dd/MM/yy", "dd-MM-yyyy", "yyyy年MM月dd日",
            "MM月dd日", "yyyy年M月d日", "M月d日", "M d, yyyy", "MMMMM dd, yyyy",
            "MM d, yyyy","MMM. dd, yyyy","MMM. d, yyyy","yyyy-MM-dd'T'HH:mm:ss-mm:ss","d MMM yyyy","dd MMM yyyy",
            "yyyy/MM/dd"

    };
    public static void main(String[] args) throws Exception{
        HttpClientResult httpClientResult = new HttpClientResult();
        String url = "https://www.militaryaerospace.com";
        if (url.startsWith("https")){
            httpClientResult = HttpClientUtil.doGet(url, true);
        } else {
            httpClientResult = HttpClientUtil.doGet(url);
        }
        System.out.println(httpClientResult.getContent());
        //System.out.println(DateUtils.formatDate(DateUtils.parseDate(dateString),dateString));
    //System.out.println(DateUtils.formatDate(DateUtils.parseDate(str)));
        /*String str = "javascript:contentView( '/NewsFocus/policies', '228800', 'pageIndex=1')";
        System.out.println(str.substring(str.indexOf("'")+1, str.indexOf(",")-1));
        System.out.println(str.substring(str.indexOf(",")+3, str.lastIndexOf(",")-1));
        String url = "https://www.korea.net/NewsFocus/policies";
        HttpClientResult httpClientResult = HttpClientUtil.doGet(url, true);
        System.out.println(httpClientResult.getContent());
        String dateString = "13/03/2023";
        String str  = "2023/03/13";
        System.out.println(DateUtils.formatDate(DateUtils.parseDate(dateString),dateString));
        System.out.println(DateUtils.formatDate(DateUtils.parseDate(str),str));
        String dateString = "112-02-01";*/
        /*
        String dateString = "112-02-01";
        MinguoDate minguo2 = MinguoDate.of(Integer.valueOf(dateString.substring(0,dateString.indexOf("-"))), Integer.valueOf(dateString.substring(dateString.indexOf("-")+1, dateString.lastIndexOf("-"))), Integer.valueOf(dateString.substring(dateString.lastIndexOf("-")+1)));
        //LocalDate localDate = LocalDate.ofEpochDay(minguo2.toEpochDay());
        LocalDate localDate2 = LocalDate.from(minguo2);
        System.out.println("MinguoDate : " + minguo2);   //105-08-24
        System.out.println("LocalDate : " + localDate2); //2016-08-24
    System.out.println(localDate2.toString());
    System.out.println(StringUtils.countString(dateString, "-"));
        String url = "https://www.cwb.gov.tw/Data/js/service/NewsHot_history.js?";
        HttpClientResult httpClientResult = HttpClientUtil.doGet(url, true);
    System.out.println(httpClientResult.getContent());
        String jsonString = httpClientResult.getContent().replaceAll("var NewsHot_history=", "");
        JSONObject dataObj = JSONObject.parseObject(jsonString);
        JSONArray jsonArray = dataObj.getJSONArray("C");
        for (int i = 0; i< jsonArray.size(); i++){
            JSONObject itemObj = jsonArray.getJSONObject(i);
            System.out.println(itemObj.getString("record_date"));
            System.out.println(itemObj.getString("title"));
            System.out.println(itemObj.getString("link"));
        }*/
        /*
        XmlReader reader = new XmlReader(new URL("https://www.space.com/feeds/all"));
        SyndFeed feed = new SyndFeedInput().build(reader);
        for (SyndEntry entry : feed.getEntries()) {4
            System.out.println(entry.toString());
            String dataKey = EncryptUtil.encrypt(entry.getLink(), "md5");
            String title = entry.getTitle();
            String content = "";
            if (entry.getContents().size() > 0){
                content = entry.getContents().get(0).getValue();
            }
            String summary = "";
            if(null != entry.getDescription()){
                summary = entry.getDescription().getValue();
            }
            String publishTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entry.getPublishedDate());
            String publishFormatTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entry.getPublishedDate());
            String url = entry.getLink();

            String avator = "";
            Html html = new Html(content);
            List<Selectable> imgNodes = html.xpath("//img/@src").nodes();
            if (imgNodes.size() > 0) {
                avator = UrlUtils.buildUrl(url, imgNodes.get(0).toString());
            }
            BizArticle article = buildArticle(dataKey, avator, title, content, summary, publishTime, publishFormatTime, url);
            System.out.println(article.toString());

        }*/
    }

    public static BizArticle buildArticle(String dataKey, String avator, String title, String content,String summary, String publishTime,String publishFormatTime, String url){
        BizArticle article = new BizArticle();
        article.setDataKey(dataKey);
        article.setAvator(avator);
        article.setTitleOrg(title);
        article.setUrl(url);
        article.setContentsOrg(content);
        article.setSummaryOrg(summary);
        article.setPublishTime(publishTime);
        article.setPublishFormatTime(publishFormatTime);
        article.setCreateTime(DateUtils.getNowDate());
        return article;
    }
}
